package com.example.piece.ui.activity

/**
 * 乐豆详情
 */

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.chad.library.adapter.base.module.BaseLoadMoreModule
import com.chad.library.adapter.base.module.LoadMoreModule
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.ext.toast
import com.example.base.ui.activity.BaseActivity
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityLeDouDetailBinding
import com.example.piece.ui.adapter.LeDouAdapter
import com.example.piece.ui.adapter.MsgAdapter
import com.example.piece.vm.UserViewModel
import com.gyf.immersionbar.ktx.immersionBar
import com.gyf.immersionbar.ktx.statusBarHeight

@Route(path = RoutePath.MAIN_LEDOU_ACTIVITY)
class LeDouDetailActivity : BaseActivity<ActivityLeDouDetailBinding>() {
    override fun getContentRes(): Int = R.layout.activity_le_dou_detail
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }
    val leftIcon1Res: Int = R.drawable.ic_white_back
    val rightIcon1Res: Int = R.drawable.ic_ledou_qs

    private val adapter: LeDouAdapter by lazy {
        val adapter = LeDouAdapter()
        mBinding.rv.adapter = adapter
        adapter
    }
    var page = 1
    var loadMoreModule: BaseLoadMoreModule? = null
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mBinding.statusBarHeight = statusBarHeight

        App.userLoginCodeEntity.info

        // Set UI title
        mBinding.tvTitle.text = "我的乐豆"

        // Init icons data
        mBinding.leftIcon1Res = leftIcon1Res
        mBinding.rightIcon1Res = rightIcon1Res
        mBinding.ivBack.setOnClickListener {
            onBackPressed()
        }
        mBinding.ivRight1.setOnClickListener {
            navigationTo(
                RoutePath.MAIN_H5_ACTIVITY, mutableMapOf(
                    Key.DataKey.H5_TITLE to "隐私协议",
                    Key.DataKey.H5_URL to "https://res.villaday.com/static/pages/1907/19/190717-19.html"
                )
            )
        }
        loadMoreModule = adapter.loadMoreModule

        mBinding.srl.setOnRefreshListener {
            page = 1
            loadMoreModule?.isEnableLoadMore = true
            loadMoreModule?.isAutoLoadMore = true
            getLeDouList()
            getLeDouInfo()        }

        immersionBar {
            fitsSystemWindows(false)
            statusBarDarkFont(true)
            //  statusBarAlpha(0.1f)
        }

        getLeDouList()
        getLeDouInfo()
    }

    private fun getLeDouInfo() {
        userViewModel.userLedouInfo(this) {
            if (it.code == 200) {
                mBinding.info = it.data

            }

        } onError {

        }
    }

    private fun getLeDouList() {
        userViewModel.userLedouList(this, page.toString()) {
            mBinding.srl.isRefreshing = false

            if (it.code == 200) {
                page++
                if (it.data.isNullOrEmpty()) {
                    adapter.loadMoreModule.isEnableLoadMore = false
                    adapter.loadMoreModule.isAutoLoadMore = false
                    return@userLedouList
                }
                if (page == 2) {
                    adapter.setList(it.data)

                } else {
                    adapter.addData(it.data)

                }

            }
        } onError {
            mBinding.srl.isRefreshing = false

        }
    }

    //签到
    fun onQianDao(view: View) {
        userViewModel.userSignin(this) {
            if (it.code == 200) {
                page = 1
                loadMoreModule?.isEnableLoadMore = true
                loadMoreModule?.isAutoLoadMore = true
                getLeDouList()
                getLeDouInfo()

            } else {
                toast(it.msg)
            }
        } onError {

        }
    }

}